package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.lang.Result;
import com.example.entity.Student;
import com.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cyk
 * @since 2022-02-19
 */
@RestController
public class StudentController {

    @Autowired
    StudentService studentService;

    /**
     * 查询所有普通用户信息
     * @return
     */
    @GetMapping("/studentAll")
    @ResponseBody
    public Result studentAll(@RequestParam(defaultValue = "1") Integer currentPage){

        Page page = new Page(currentPage, 5);
        QueryWrapper queryWrapper = new QueryWrapper<Student>();
        IPage pageData = studentService.page(page, queryWrapper);

        return Result.succ(pageData);
    }

    /**
     * 根据用户id获取用户的信息
     * @param id
     * @return
     */
    @GetMapping("/student/{user_id}")
    public Result list(@PathVariable(name = "user_id") Long id){

        QueryWrapper queryWrapper = new QueryWrapper<Student>().eq("user_id",id);
        Student student = studentService.getOne(queryWrapper);

        return Result.succ(student);
    }

    @GetMapping("/stu/{id}")
    public Result stu(@PathVariable(name = "id") Long id){

        Student student = studentService.studentById(id);

        return Result.succ(student);
    }

    @PostMapping("/studentEdit")
    public Result studentEdit(@Validated @RequestBody Student student){

        Student temp = new Student();
        temp.setId(student.getId());
        temp.setUserId(student.getUserId());
        temp.setName(student.getName());
        temp.setSex(student.getSex());
        temp.setAge(student.getAge());
        temp.setPhone(student.getPhone());
        temp.setEmail(student.getEmail());
        temp.setAddress(student.getAddress());
        temp.setPolitical(student.getPolitical());

        boolean i = studentService.updateStudent(temp);

        return Result.succ(null);
    }

}
